package com.oa.ylms.apps.dao.file.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.oa.ylms.apps.constants.IDEDStates;
import com.oa.ylms.apps.dao.file.IFileInfoDao;
import com.oa.ylms.apps.entity.file.FileInfoValue;

public class FileInfoDaoImpl extends HibernateDaoSupport implements IFileInfoDao {

	private List<FileInfoValue> list;
	
	private Integer numbers;
	
	// DAO method

	@Override
	public int addFileInfo(FileInfoValue fileInfo) throws Exception {
		if (fileInfo != null) {
			this.getHibernateTemplate().save(fileInfo);
			return fileInfo.getFileId();
		}
		return 0;
	}


	@SuppressWarnings("unchecked")
	@Override
	public int countTPages(final FileInfoValue fileInfo,int rows) throws Exception {
			int pageNumbers = 0;
			String hqlStr = "FROM FileInfoValue fiv WHERE 1=1";
			if (fileInfo.getFileCategory() != null) {
				hqlStr += " AND fiv.fileCategory = ?";
			}
			if(fileInfo.getTitle() != null){
				hqlStr += " AND fiv.title like ?";
			}
			if(fileInfo.getCreateTime() != null){
				hqlStr += " AND fiv.createTime <= ?";
			}
			if(fileInfo.getStates() != null){
				hqlStr += " AND fiv.states = ?";
			}
			hqlStr += " AND fiv.states <> ? ORDER BY fiv.createTime DESC";
			final String hql = hqlStr;
			list = this.getHibernateTemplate().executeFind(
					new HibernateCallback() {
						public Object doInHibernate(Session session)
								throws HibernateException, SQLException {
							Query query = session.createQuery(hql);
							int index = -1;
							if (fileInfo.getFileCategory() != null) {
								query.setEntity(++index, fileInfo.getFileCategory());
							}
							if(fileInfo.getTitle() != null){
								query.setString(++index, fileInfo.getTitle());
							}
							if(fileInfo.getCreateTime() != null){
								query.setDate(++index, fileInfo.getCreateTime());
							}
							if(fileInfo.getStates() != null){
								query.setInteger(++index, fileInfo.getStates());
							}
							query.setInteger(++index, IDEDStates.DELETE);
							list = query.list();
							return list;
						}
					});
			numbers = list.size();
			if (numbers <= rows) {
				pageNumbers = 1;
			} else {
				if (numbers % rows == 0) {
					pageNumbers = numbers / rows;
				} else {
					pageNumbers = numbers / rows + 1;
				}
			}
		return pageNumbers;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<FileInfoValue> qureyFileInfoWithPage(final FileInfoValue fileInfo ,final int rows, final int pageNo)
			throws Exception {
			String hqlStr = "FROM FileInfoValue fiv WHERE 1=1";
			if (fileInfo.getFileCategory() != null) {
				hqlStr += " AND fiv.fileCategory = ?";
			}
			if(fileInfo.getTitle() != null){
				hqlStr += " AND fiv.title like ?";
			}
			if(fileInfo.getCreateTime() != null){
				hqlStr += " AND fiv.createTime <= ?";
			}
			if(fileInfo.getStates() != null){
				hqlStr += " AND fiv.states = ?";
			}
			hqlStr += " AND fiv.states <> ? ORDER BY fiv.createTime DESC";
			final String hql = hqlStr;
			list = this.getHibernateTemplate().executeFind(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						Query query = session.createQuery(hql);
						int index = -1;
						if (fileInfo.getFileCategory() != null) {
							query.setEntity(++index, fileInfo.getFileCategory());
						}
						if(fileInfo.getTitle() != null){
							query.setString(++index, fileInfo.getTitle());
						}
						if(fileInfo.getCreateTime() != null){
							query.setDate(++index, fileInfo.getCreateTime());
						}
						if(fileInfo.getStates() != null){
							query.setInteger(++index, fileInfo.getStates());
						}
						query.setInteger(++index, IDEDStates.DELETE);
						query.setFirstResult(rows * (pageNo - 1));
						query.setMaxResults(rows);
						list = query.list();
						return list;
					}
				});
			return list;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<FileInfoValue> qureyFileInfo(final FileInfoValue fileInfo) throws Exception{
		String hqlStr = "FROM FileInfoValue fiv WHERE 1=1";
		if(fileInfo.getStates() != null){
			hqlStr += " AND fiv.states = ?";
		}
		hqlStr += " AND fiv.states <> ? ORDER BY fiv.createTime DESC";
		final String hql = hqlStr;
		list = this.getHibernateTemplate().executeFind(
			new HibernateCallback() {
				public Object doInHibernate(Session session)
						throws HibernateException, SQLException {
					Query query = session.createQuery(hql);
					int index = -1;
					if(fileInfo.getStates() != null){
						query.setInteger(++index, fileInfo.getStates());
					}
					query.setInteger(++index, IDEDStates.DELETE);
					list = query.list();
					return list;
				}
			});
		return list;
	}

	@Override
	public FileInfoValue qureyFileById(FileInfoValue fileInfo) throws Exception {
		if (fileInfo != null) {
			return (FileInfoValue) this.getHibernateTemplate().get(
					FileInfoValue.class, fileInfo.getFileId());
		}
		return null;
	}
	
	@Override
	public boolean editFileInfo(FileInfoValue fileInfo) throws Exception {
		if (fileInfo != null) {
			try {
				this.getHibernateTemplate().update(fileInfo);
			} catch (Exception e) {
				return false;
			}
			return true;
		}
		return false;
	}
    
	@Override
	public int getTotalNumbers() throws Exception{
		return getNumbers();
	}
	
	// DAO property accessors
	
	public List<FileInfoValue> getList() {
		return list;
	}

	public void setList(List<FileInfoValue> list) {
		this.list = list;
	}


	public void setNumbers(Integer numbers) {
		this.numbers = numbers;
	}


	public Integer getNumbers() {
		return numbers;
	}
	
}
